package com.app.shop.service.shop.impl;

import com.app.shop.dao.wg.WgPictureMapper;
import com.app.shop.entity.WgPicture;
import com.app.shop.entity.WgPicture;
import com.app.shop.entity.WgPictureExample;
import com.app.shop.entity.query.WgPictureQuery;
import com.app.shop.service.shop.WgPictureService;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 图片资源服务
 */
@Service("wxArticleService")
public class WgPictureServiceImp implements WgPictureService {

    private static final Logger log = LoggerFactory.getLogger(WgPictureServiceImp.class);

    @Autowired
    private WgPictureMapper wgPictureMapper;

    @Override
    public Page<WgPicture> getWgPictureList(WgPictureQuery wgPictureQuery) {
        //查询条件
        WgPictureExample wgPictureExample = new WgPictureExample();
        WgPictureExample.Criteria criteria = wgPictureExample.createCriteria();
        criteria.andDeleteFlagEqualTo(1);
        //图片名称
        if (wgPictureQuery.getName() != null && wgPictureQuery.getName().trim().length() > 0) {
            criteria.andNameLike('%' + wgPictureQuery.getName() + '%');
        }
        //模拟分页查询
        PageHelper.startPage(wgPictureQuery.getCurrentPage(), wgPictureQuery.getPageSize());
        return wgPictureMapper.selectByExample(wgPictureExample);
    }

    @Override
    public List<WgPicture> getWgPictureAllList(WgPictureQuery wgPictureQuery) {
        //查询条件
        WgPictureExample wgPictureExample = new WgPictureExample();
        WgPictureExample.Criteria criteria = wgPictureExample.createCriteria();
        criteria.andDeleteFlagEqualTo(1);
        return wgPictureMapper.selectByExample(wgPictureExample);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addPicture(WgPicture wgPicture) {
        wgPicture.setStatus(1);
        wgPicture.setDeleteFlag(1);
        wgPictureMapper.insert(wgPicture);
        Constant.PICTURE_MAP.put(wgPicture.getId(),wgPicture.getUrl());

    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editPictureDelete(WgPicture wgPicture) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgPictureExample wgPictureExample = new WgPictureExample();
            WgPictureExample.Criteria criteria = wgPictureExample.createCriteria();
            criteria.andIdEqualTo(wgPicture.getId()).andDeleteFlagEqualTo(1);
            Long cnt = wgPictureMapper.countByExample(wgPictureExample);
            if (cnt>0) {
                wgPicture.setModifiedUserId(wgPicture.getModifiedUserId());
                wgPicture.setModifiedDate(wgPicture.getModifiedDate());
                wgPicture.setStatus(2);
                wgPicture.setDeleteFlag(2);
                wgPictureMapper.updateByExample(wgPicture, wgPictureExample);
                Constant.PICTURE_MAP.remove(wgPicture.getId());
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("商品修改失败，删除商品必须为有效的商品！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品修改失败，修改过程发生异常请重试！");
            log.error("商品修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    public void initWgPictureAllList() {
        //查询条件
        WgPictureExample wgPictureExample = new WgPictureExample();
        WgPictureExample.Criteria criteria = wgPictureExample.createCriteria();
        criteria.andDeleteFlagEqualTo(1);
        List<WgPicture> list= wgPictureMapper.selectByExample(null);
        for(WgPicture wgPicture:list){
            Constant.PICTURE_MAP.put(wgPicture.getId(),wgPicture.getUrl());
        }
    }
}
